(function () {
    'use strict';
    angular.module('charity.article', []).factory('$articleService', ['$http', function ($http) {
        return {

            'articleTypeList': [
                {
                    id: 10,
                    label: '机构动态'
                },
                {
                    id: 11,
                    label: '项目动态'
                },
                {
                    id: 12,
                    label: '行业动态'
                },
                {
                    id: 13,
                    label: '微爱'
                }
                ,
                {
                    id: 14,
                    label: '乐活'
                }],
            'query': function (params) {
                return $http({
                    method: 'GET',
                    url: '/articles/',
                    params: params
                });
            },
            //只查询媒体报道
            'queryReport': function () {
                return $http({
                    method: 'GET',
                    url: '/articles/report'
                });
            },
            'save': function (article) {
                return $http({
                    method: 'POST',
                    url: '/articles',
                    params: article
                });
            },
            'update': function (article) {
                return $http({
                    method: 'PUT',
                    url: '/articles/' + article.id,
                    params: article
                });
            },
            'getArticleEditor': function (domId) {
                var domId = domId || 'editor-trigger';
                var editor = new wangEditor(domId);
                editor.config.menuFixed = false;
                //上传图片
                //editor.config.uploadImgUrl='/upload';
                editor.config.menus = [
                    /*        'source',
                     '|',*/
                    'bold',
                    'underline',
                    'italic',
                    'strikethrough',
                    'eraser',
                    'forecolor',
                    'bgcolor',
                    '|',
                    'quote',
                    'fontfamily',
                    'fontsize',
                    'head',
                    'unorderlist',
                    'orderlist',
                    'alignleft',
                    'aligncenter',
                    'alignright',
                    '|',
                    'link',
                    'unlink',
                    'table',
                    /*        'emotion',*/
                    '|',
                    'img',
                    'video',
                    /*        'location',
                     'insertcode',*/
                    '|',
                    'undo',
                    'redo',
                    'fullscreen'
                ];

                //$uploadContent
                editor.config.customUpload = true;

                var uploadInit = function () {
                    var editor = this;
                    // 编辑器中，触发选择图片的按钮的id
                    var btnId = editor.customUploadBtnId;
                    // 编辑器中，触发选择图片的按钮的父元素的id
                    var containerId = editor.customUploadContainerId;
                    var $input = $('<input type="file" accept="image/*" />');
                    var containerInput = $('<div style="visibility:hidden;"></div>');
                    containerInput.append($input);
                    editor.customUploadInputBtn = $input;
                    $('body').append(containerInput);

                    // onchange 事件
                    $input.on('change', function (e) {
                        var file = e.currentTarget.files[0];
                        if (file) {
                            var avFile = new AV.File(file.name, file);
                            avFile.save().then(function () {
                                editor.command(null, 'insertHtml', '<img src="' + avFile.url() + '" style="max-width:100%;"/>');
                                console.log("上传成功");
                                editor.customUploadInputBtn.val('');
                            }, function (error) {
                                console.log("上传失败");
                                editor.customUploadInputBtn.val('');
                            })
                        } else {
                            console.log('doesnot select file');
                            editor.customUploadInputBtn.val('');
                        }
                    });

                    // 选择本地文件，上传
                    $('#' + btnId).click(function (e) {
                        $input.click();
                        e.preventDefault();
                    });
                };

                editor.config.customUploadInit = uploadInit;
                editor.create();
                return editor;
            }
        }
    }]);
})();
